package Example.Components;

import java.util.Date;
import java.util.Vector;

import Example.Interfaces.BQuery;
import Example.Interfaces.BSave;

public class BikeTripData implements BQuery, BSave {
	
	public Vector bikes=new Vector();
	public Vector courses=new Vector();
	public BikeTripData(){
		/*
		 * bike information: bikeId(int), bikeTripSum(double),bikeLocation(string) 
		 * bikeState(false): true(under rent), false(in stock)
		 */
		Object[]b1={Integer.valueOf(001),Double.valueOf(100.5),"Gare",Boolean.valueOf(true)};
		Object[]b2={Integer.valueOf(002),Double.valueOf(10.6),"Triolet",Boolean.valueOf(false)};
		Object[]b3={Integer.valueOf(003),Double.valueOf(11.5),"Gare",Boolean.valueOf(false)};
		Object[]b4={Integer.valueOf(004),Double.valueOf(30.6),"Triolet",Boolean.valueOf(false)};
		Object[]b5={Integer.valueOf(005),Double.valueOf(150.5),"Gare",Boolean.valueOf(false)};
		Object[]b6={Integer.valueOf(006),Double.valueOf(1.6),"Triolet",Boolean.valueOf(false)};
		bikes.add(b1);
		bikes.add(b2);
		bikes.add(b3);
		bikes.add(b4);
		bikes.add(b5);
		bikes.add(b6);
		/*
		 * course information: userId(int), userTripSum(double), 
		 * userState(boolean):true(under rent), false(free)
		 * if userstae=true, userBike(bikeID:int)
		 * rentTime
		 */
		Object[]u1={Integer.valueOf(001),Double.valueOf(10),Boolean.valueOf(true),Integer.valueOf(001),
				"1232550255063"};
		Object[]u2={Integer.valueOf(002),Double.valueOf(20),Boolean.valueOf(false),Integer.valueOf(0),""};
		Object[]u3={Integer.valueOf(003),Double.valueOf(30),Boolean.valueOf(false),Integer.valueOf(0),""};
		courses.add(u1);
		courses.add(u2);
		courses.add(u3);
	}
	public boolean saveBike(int bikeID, String location, double trip) {

		Object[] b=null, a= null;
		int j = 0;
	
			for (int i = 0; i < bikes.size(); i++) {

				b=(Object[]) bikes.get(i);
				if(trip==0){
					if(((Integer)b[0]).intValue()==bikeID&&b[3].toString().equals("false")){
						j=i;
						b[3]=Boolean.valueOf(true);	
						a=b;
					}
				}
				else{

					if(((Integer)b[0]).intValue()==bikeID&&b[3].toString().equals("true")){
						j=i;
						b[3]=Boolean.valueOf(false);	
						double d=Double.parseDouble(b[1].toString())+trip;
						
						b[1]=Double.valueOf(d);
						b[2]=location;
						a=b;
						/*for (int h = 0; h < a.length; h++) {
							System.out.println("eeeee"+a[h]);
						}*/
					}
				}
		}
			bikes.add(j, a);
		return false;
	}

	public String saveCourse(int userID, int bikeID) {
		Object[] a = null, b=null;
		String time = null;
		int j=88888888;

		for (int i = 0; i < courses.size(); i++) {
			b=(Object[]) courses.get(i);
			if(((Integer)b[0]).intValue()==userID&&b[2].toString().equals("false")){
				
				b[2]=Boolean.valueOf(true);
				b[3]=Integer.valueOf(bikeID);
				long l=System.currentTimeMillis();
				Date s=new Date(l);
				b[4]=s.toString();
				j=i;
				a=b;

				
			}
			else if(((Integer)b[0]).intValue()==userID&&b[2].toString().equals("true")){
				b[2]=Boolean.valueOf(false);
				b[3]=Integer.valueOf(0);
				time=(String) b[4];
				b[4]="";
				j=i;
				a=b;
			}
		}
		if(j!=88888888){
	
			courses.add(j, a);
			Object[] c=(Object[]) courses.get(1);

			return time;
		}
		
		return "";
	}

	public Vector findBike(String locationStart) {
		
		Vector bikesF = new Vector() ;				
		
		if(locationStart.equals("Gare")|locationStart.equals("Triolet")){
			for (int i = 0; i < bikes.size(); i++) {
				Object[] b=(Object[]) bikes.get(i);
				if(b[2].equals(locationStart)&&b[3].equals(Boolean.valueOf(false)))
					bikesF.add(b);
			}
		}
		else{
			for (int i = 0; i < bikes.size(); i++) {
				Object[] b=(Object[]) bikes.get(i);
				if(b[3].equals(Boolean.valueOf(false)))
					bikesF.add(b);
			}
		}
		return bikesF;
	}

	public Object[] findCourse(int userID) {
		Object[] c=null;
		for (int i = 0; i < courses.size(); i++) {
			c=(Object[]) courses.get(i);
			if(((Integer)c[0]).intValue()==userID){
				System.out.println(c[4]);
				return c;
			}
		}
		return null;
	}

}
